<?php

/**
 * Created by PhpStorm.
 * User: zuoa
 * Date: 2016/11/25 0025
 * Time: 10:34
 */
class Accout_model extends CI_Model
{
    public function __construct()
    {
        $this->load->database();
    }

    private function _encrypt_password($p)
    {
        return md5(md5(md5($p) . "Uniview") . "y00486");
    }

    public function login($username, $password)
    {
        $password_encrypt = $this->_encrypt_password($password);
        $query = $this->db->get_where("tbl_accout", array("username" => $username, "password" => $password_encrypt));
        return $query->first_row();
    }

    public function add_account($username, $password)
    {
        $password_encrypt = $this->_encrypt_password($password);
        $account = array("username" => $username, "password" => $password_encrypt);
        $this->db->insert("tbl_accout", $account);
    }

    public function modify_password($username, $password, $new_password)
    {
        $password_encrypt = $this->_encrypt_password($password);
        $new_password_encrypt = $this->_encrypt_password($new_password);
        $account = array("username" => $username, "password" => $password_encrypt);
        $this->db->update("tbl_accout", array("password"=>$new_password_encrypt), $account);
        return $this->db->affected_rows();
    }
}